﻿using ASP;
using HKB.Common;
using HKB.Web;
using HKB.Web.Shop.Member;
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;

public partial class Member_Shop_ProductInStock : Member, IRequiresSessionState
{
    private string linkHead = "";
    private int UserID = 0;
    private string wherestr = "";
    private string orderstr = " order by ProductID Desc";
    private ArrayList al = new ArrayList();
    private IDbDataParameter[] param = null;
    private Pagination _pageHand = null;

	protected DefaultProfile profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax applicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
    protected Pagination pageHand
    {
        get
        {
            Pagination pageHand;
            if (this._pageHand == null)
            {
                this._pageHand = new Pagination();
                this._pageHand.SQL_CloseTablePre = false;
                this._pageHand.SQL_Fields = "*,(Select ClassName From [HLShop_Class] Where ClassID=HLShop_Product.ClassID) as ClassName";
                this._pageHand.P_Object = Language.GetAdminMsg("Product/ProductName");
                pageHand = this._pageHand;
            }
            else
            {
                pageHand = this._pageHand;
            }
            return pageHand;
        }
    }

	protected void Page_Load(object sender, EventArgs e)
    {
        this.UserID = base.user.UserID;
		if (!base.IsPostBack)
		{
			this.labPage.Text = "1";
            this.GridViewDataBound();
		}
	}

    protected void GridViewDataBound()
    {
        this.QuestString();
        Shop_Product shop_Product = new Shop_Product(this.conn);
        if (this.al.Count > 0)
        {
            this.param = new IDbDataParameter[this.al.Count];
            this.al.CopyTo(this.param, 0);
        }
        DataTable dataTable = new DataTable();
        if (this.param == null)
        {
            dataTable = shop_Product.GetInStockBySalerID(this.UserID, this.wherestr, this.orderstr, null);
        }
        else
        {
            dataTable = shop_Product.GetInStockBySalerID(this.UserID, this.wherestr, this.orderstr, this.param);
        }

        //获取数据源
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = dataTable.DefaultView;
        //设置允许分页
        pds.AllowPaging = true;
        //设置在单页上显示的条数
        pds.PageSize = 2;
        //获取或设置当前页的索引
        pds.CurrentPageIndex = Int32.Parse(labPage.Text) - 1;

        //设置页的总数
        LabCountPage.Text = pds.PageCount.ToString();
        //设置按钮的可操作性
        lbtnDownPage.Enabled = true;
        lbtnFirstPage.Enabled = true;
        lbtnNextPage.Enabled = true;
        lbtnpritPage.Enabled = true;
        if (pds.CurrentPageIndex < 1)
        {
            lbtnFirstPage.Enabled = false;
            lbtnpritPage.Enabled = false;
        }
        if (pds.CurrentPageIndex == pds.PageCount - 1)
        {
            lbtnDownPage.Enabled = false;
            lbtnNextPage.Enabled = false;
        }
        this.Repeater1.DataSourceID = "";
        this.Repeater1.DataSource = pds;
        this.Repeater1.DataBind();
    }

    protected void QuestString()
    {
        this.wherestr += "and IsDeleted=0  ";
        string text = this.ProductName.Text;
        if (text != string.Empty)
        {
            this.wherestr += "and ProductName like '%' + @ProductName + '%' ";
            this.al.Add(this.conn.CreateParameter("@ProductName", DbType.String, text.Trim()));
        }
        if (this.ProductType.SelectedValue != "" && this.ProductType.SelectedValue != "0")
        {
            this.wherestr = this.wherestr + "and ProductType=" + this.ProductType.SelectedValue + " ";
        }
        if (this.wherestr != string.Empty)
        {
            this.wherestr = "and" + this.wherestr;
            this.wherestr = this.wherestr.Replace("andand", "Where");
        }
    }
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string text = Text.CheckNumList(Fetch.GetOrPost("Product_Select"));
		Shop_Product shop_Product = new Shop_Product(this.conn);
		if (text != "")
		{
			string text2 = text;
			string[] array = text2.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
				if (e.CommandName.ToString() == "StopSale")
				{
					int num = this.conn.Execute("delete HL_GroupShopProduct where MainProductID =" + int.Parse(array[i]));
				}
			}
			int num2 = shop_Product.EditSaleProductProtity(text2, e.CommandName.ToString());
			this.GridViewDataBound();
		}
	}

	protected void QucikSearch_Click(object sender, EventArgs e)
	{
        this.GridViewDataBound();
	}

	protected void btn_editProducts_Click(object sender, EventArgs e)
	{
		string text = Text.CheckNumList(Fetch.GetOrPost("Product_Select"));
		if (text == "")
		{
			this.WriteErrMsg("", "未选中编辑项", "");
		}
		else
		{
			base.Response.Redirect("EditProducts.aspx?tag=Sale&productIDS=" + text);
		}
	}

	//设置首页按钮功能
	protected void lbtnFirstPage_Click(object sender, EventArgs e)
	{
		labPage.Text = "1";
		this.GridViewDataBound();
	}
	//设置上一页按钮功能
	protected void lbtnpritPage_Click(object sender, EventArgs e)
	{
		labPage.Text = (Int32.Parse(labPage.Text) - 1).ToString();
		this.GridViewDataBound();
	}
	//设置下一页按钮功能
	protected void lbtnNextPage_Click(object sender, EventArgs e)
	{
		labPage.Text = (Int32.Parse(labPage.Text) + 1).ToString();
		this.GridViewDataBound();
	}
	//设置尾页按钮功能
	protected void lbtnDownPage_Click(object sender, EventArgs e)
	{
		labPage.Text = LabCountPage.Text;
		this.GridViewDataBound();
	}

	protected void Repeater1_ItemDataBound(object source, RepeaterCommandEventArgs e)
    {
		string text = e.CommandArgument.ToString();
		Shop_Product shop_Product = new Shop_Product(this.conn);
		if (text != "")
		{
			string text2 = text;
			string[] array = text2.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
                if (e.CommandName.ToString() == "MoreDelete")
				{
                    int num = 0;
                    int productidDel = int.Parse(array[i]);
                    num = this.DelProduct(productidDel);
                    if (num > 0)
                    {
                        this.WriteErrMsg("", "批量信息删除成功！","");
                    }
				}
                if (e.CommandName == "OnSale")
                {
                    DataSet dataSet = this.conn.GetDataSet("select * from HLShop_Product where ProductID=" + int.Parse(array[i]));
                    if (dataSet.Tables[0].Rows[0]["ProductType"].ToString() == "2")
                    {
                        this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('" + dataSet.Tables[0].Rows[0]["ProductName"].ToString() + " 属于拍卖成功商品,如果想继续拍卖请删除再重新发布！');</script>");
                        return;
                    }
                    if (int.Parse(dataSet.Tables[0].Rows[0]["Stocks"].ToString()) <= 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(base.GetType(), "Test", "<script>alert('" + dataSet.Tables[0].Rows[0]["ProductName"].ToString() + " 的数量为0，无法上架！');</script>");
                        return;
                    }
                    dataSet.Dispose();
                }
			}

			int num2 = shop_Product.EditSaleProductProtity(text2, e.CommandName.ToString());
			this.GridViewDataBound();
		}
	}

    private int DelProduct(int productidDel)
    {
        string sql = "select ProductSmallImageUrl from HLShop_Product where ProductID=" + productidDel;
        DataTable dataTable = this.conn.GetDataTable(sql);
        //if (dataTable.Rows.Count > 0)
        //{
        //    string text = dataTable.Rows[0]["ProductSmallImageUrl"].ToString();
        //    if (text != null && text != "")
        //    {
        //        string path = base.Server.MapPath("../" + text);
        //        if (File.Exists(path))
        //        {
        //            File.Delete(path);
        //        }
        //    }
        //}
        //int num = this.conn.Execute("delete HL_GroupShopProduct where MainProductID=" + productidDel);
        //int num2 = this.conn.Execute("delete HLShop_ProductImg where ProductID=" + productidDel);
        //return this.conn.Execute("delete HLShop_Product where ProductID=" + productidDel);
        return 1;
    }
}
